package com.example.autumn.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.example.autumn.entity.Account;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

/**
 * @author Liaorun
 */
@Mapper
public interface AccountMapper extends BaseMapper<Account> {

    /**
     * 分布式事务TCC 解法的 try 方法
     *
     * @param userId 用户id
     * @param money 金额
     * @return
     */
    @Update("update account set money = money - ${money} where user_id = #{userId}")
    int deduct(String userId, int money);


    /**
     * 分布式事务TCC 解法的 cancel 回滚方法
     *
     * @param userId 用户id
     * @param money 金额
     * @return
     */
    @Update("update account set money = money + ${money} where user_id = #{userId}")
    int refund(String userId, BigDecimal money);
}
